<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>东方财富概念板块监控</title>
    <link th:href="@{/webjars/bootstrap/css/bootstrap.min.css}" rel="stylesheet" />
    <style>
        body { padding: 20px; background: #f8f9fa; }
        .board-table { background: white; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }
        .up { color: #dc3545; }
        .down { color: #0d6efd; }
        .header { cursor: pointer; user-select: none; }
        .header:hover { background: #f0f0f0; }
        .search-box { max-width: 300px; margin-bottom: 15px; }
        .loading { font-style: italic; color: #6c757d; }
    </style>
</head>
<body>
<div class="container">
    <h2 class="text-center mb-4">📊 东方财富概念板块实时监控</h2>
    <p class="text-center text-muted">最后更新: <span id="lastUpdate">加载中...</span></p>

    <div class="d-flex justify-content-between mb-3">
        <input type="text" id="searchInput" class="form-control search-box" placeholder="搜索板块名称..." />
        <button class="btn btn-outline-secondary btn-sm" onclick="refreshData()">
            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
                <path d="M8 3a5 5 0 1 1-4.546 2.914.5.5 0 0 0-.908-.417A6 6 0 1 0 8 2z"/>
                <path d="M8 4.466V.534a.5.5 0 0 0-1 0v3.932a.5.5 0 0 0 1 0z"/>
            </svg>
            刷新
        </button>
    </div>

    <div class="table-responsive">
        <table class="table table-hover board-table">
            <thead class="table-light">
            <tr>
                <th onclick="sortTable('rank')" class="header">排名</th>
                <th onclick="sortTable('name')" class="header">板块名称</th>
                <th onclick="sortTable('code')" class="header">代码</th>
                <th onclick="sortTable('changePercent')" class="header">涨幅</th>
                <th onclick="sortTable('totalStocks')" class="header">个股数</th>
                <th onclick="sortTable('netInflow')" class="header">净流入(万元)</th>
                <th>领涨股</th>
                <th>第二领涨</th>
            </tr>
            </thead>
            <tbody id="boardBody">
            <tr><td colspan="8" class="text-center loading">加载中...</td></tr>
            </tbody>
        </table>
    </div>
</div>

<script th:src="@{/webjars/jquery/jquery.min.js}"></script>
<script th:src="@{/webjars/bootstrap/js/bootstrap.bundle.min.js}"></script>
<script>
    let currentSort = 'changePercent,desc';

    function sortTable(field) {
        const direction = currentSort.startsWith(field) && currentSort.endsWith('asc') ? 'desc' : 'asc';
        currentSort = `${field},${direction}`;
        fetchData();
    }

    function fetchData() {
        const search = $('#searchInput').val().toLowerCase();
        $.getJSON('/api/boards?sort=' + currentSort)
            .done(data => {
                const filtered = search ? data.filter(b => b.name.toLowerCase().includes(search)) : data;
                renderTable(filtered);
                $('#lastUpdate').text('加载于 ' + new Date().toLocaleTimeString());
            })
            .fail((jqXHR, textStatus, errorThrown) => {
                $('#boardBody').html(`<tr><td colspan="8" class="text-center text-danger">加载失败: ${textStatus}</td></tr>`);
            });
    }

    function refreshData() {
        fetchData();
    }

    function renderTable(data) {
        let html = '';
        data.forEach(b => {
            const changeClass = b.changePercent >= 0 ? 'up' : 'down';
            const inflowClass = b.netInflow >= 0 ? 'up' : 'down';
            html += `
            <tr>
                <td><strong>${b.rank}</strong></td>
                <td><strong>${b.name}</strong></td>
                <td>${b.code}</td>
                <td class="${changeClass}">${b.changePercent.toFixed(2)}%</td>
                <td>${b.totalStocks}</td>
                <td class="${inflowClass}">${formatNumber(b.netInflow)}万</td>
                <td>${b.leadingStockName || '-'}<br><small>${b.leadingStockCode || ''} (${formatChange(b.leadingStockChangePercent)})</small></td>
                <td>${b.secondLeadingStockName || '-'}<br><small>${b.secondLeadingStockCode || ''} (${formatChange(b.secondLeadingStockChangePercent)})</small></td>
            </tr>`;
        });
        $('#boardBody').html(html || '<tr><td colspan="8" class="text-center">无匹配数据</td></tr>');
    }

    function formatChange(val) {
        if (val == null) return '-';
        return val >= 0 ? `<span class="up">+${(val/100).toFixed(2)}%</span>` : `<span class="down">${(val/100).toFixed(2)}%</span>`;
    }

    function formatNumber(num) {
        return num > 0 ? '+' + num.toLocaleString() : num.toLocaleString();
    }

    // 页面加载完成后自动拉取数据
    $(function() {
        fetchData();
        // 每10秒自动刷新
        setInterval(fetchData, 10000);
    });

    // 搜索功能
    $('#searchInput').on('input', fetchData);
</script>
</body>
</html>
